package com.xiaolin.base.common;

/**
 * @ClassName Fibonacci
 * @Description 斐波那契数列，经典的题目就是兔子问题
 * @Detail detail
 * @Author MyPC
 * @Date 2020/7/23
 * @Version 1.0
 */
public class Fibonacci {
    public static void main(String[] args) {
        System.out.println(rabbit01(6));
        System.out.println(rabbit02(6));
    }
    //循环计算斐波那契数列
    public static int rabbit01(int mouth){
        if(mouth<=2){
            return 1;
        }
        int one=1;
        int two=1;
        //临时变量，用来存储该月的上个月的兔子数
        int temp;
        for(int i=3;i<=mouth;i++){
            temp=two;
            two+=one;
            one=temp;
        }
        return two;
    }
    //递归计算斐波那契数列
    public static int rabbit02(int mouth){
        if(mouth <=2){
            return 1;
        }
        return rabbit02(mouth-1)+rabbit01(mouth-2);
    }
}
